using System.CodeDom.Compiler;
using System.Globalization;
using Microsoft.Practices.EnterpriseLibrary.Validation;

namespace PrismFactory.SoftwareFactory.Views.Validators
{
    public class IdentifierValidator : Validator<string>
    {
        public IdentifierValidator()
            : base(null, null)
        {
        }

        protected override void DoValidate(string objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            CodeDomProvider provider = CodeDomProvider.CreateProvider("CS");
            if (!provider.IsValidIdentifier(objectToValidate))
            {
                string message = string.Format(CultureInfo.CurrentCulture, MessageTemplate, objectToValidate);
                LogValidationResult(validationResults, message, currentTarget, key);
            }
        }

        protected override string DefaultMessageTemplate
        {
            get { return "The value {0} is not a valid identifier"; }
        }
    }
}